<?php

class Qlquangcao extends CI_Controller {

    function __construct() {
        parent::__construct();
        if ($this->session->userdata("logged_in") != true) {
            header("location:./");
        }
        $this->load->Model("Mqlquangcao", 'quangcao');
    }

    public function index() {
        $this->viewall();
    }
    function deletequangcao()
	{
		$id = $this->input->post('id');
		$this->quangcao->delete($id);
		exit(json_encode(array(
            'msg' => 'Delete topic success!'
        )));
		
	}
    public function viewall($sort = 'id', $order = 'DESC', $name = '0', $limit = '5', $start = '0') {
        $name = urldecode($name);
        $filter = array(
            'sort' => $sort,
            'order' => $order,
            'name' => $name,
            'start' => $start,
            'limit' => $limit
        );
        $data['quangcao'] = $this->quangcao->listAll($filter);
        $totalquangcao = $this->quangcao->totalquangcao($filter);
        $data['totalquangcao'] = $totalquangcao;
        $data['name'] = $name;
        $data['sort'] = $sort;
        $data['start'] = $start;
        $data['limit'] = $limit;
        $data['order'] = $order;

        // phân trang cho app
        $this->load->library('pagination');
        $config['base_url'] = site_url("qltintuc/viewAll/$sort/$order/$name/$limit");
        $config['total_rows'] = $totalquangcao;
        $config['uri_segment'] = 10;
        $config['per_page'] = $limit;
        $config['full_tag_open'] = '<div class="pagination"><div class="links">';
        $config['full_tag_close'] = '</div></div>';
        $config['cur_tag_open'] = '<span class="active curved">';
        $config['cur_tag_close'] = '</span>';
        $config['num_tag_open'] = '';
        $config['num_tag_close'] = '';
        $config['next_link'] = 'Next';
        $config['prev_link'] = 'Previous';
        $config['prev_tag_open'] = '';
        $config['prev_tag_close'] = '';
        $config['last_tag_open'] = '';
        $config['last_tag_close'] = '';
        $this->pagination->initialize($config);

        if (isset($this->msg['error']))
            $data['error'] = $this->msg['error'];
        if (isset($this->msg['success']))
            $data['success'] = $this->msg['success'];
        $this->load->helper('form');
        $data["username"] = $this->session->userdata('username');
        $this->load->view(__TEMPLATES_PATH__ . "/admin/qlquangcao", $data);
    }
    	function delete()
	{
		if(isset($_POST['selected'])) {
			foreach($_POST['selected'] as $id)
				$this->quangcao->delete($id);
			$this->msg['success'] = 'Đã xóa topic (s) thành công';	
			
		}
		
		
		
		$this->viewAll();
		
	}
        
        function edit($id)
	{
		if(isset($_POST['update'])) {
            $oldUmask = umask();
            umask(0);
			$cur = date('mY', microtime(true));
			$path = '/' . UPLOADFOLDER . '/quangcao/' . $cur . '/';
            if(!is_dir('.' . $path)) mkdir('.' . $path);
            umask($oldUmask);
			$config['upload_path'] = '.' . $path;
			$config['allowed_types'] = 'jpg|png|gif';
			$this->load->library('upload', $config);
			$image = "";
			if(!$this->upload->do_upload('image')) {
				// $data['error'] = $this->upload->display_errors();
			} else {
				$upload = $this->upload->data();
				$image = $path . $upload['file_name'];
                
                $imagesize = getimagesize('.'.$image);
                //Load thư viện xử lý ảnh để tạo thumbnail
                $this->load->library('image_lib');
                $config['source_image']	= '.'.$image;
                $config['width']    = $imagesize[0];
                $config['height']   = $imagesize[1];
                $config['quality']  = '100%';
                $this->image_lib->initialize($config);
                $this->image_lib->resize();
                $this->image_lib->clear();
                $_POST['image'] = $image;
			}
			$this->load->library('upload_multi');
	        $res = json_decode($this->upload_multi->do_upload());
	        if($res && $res->status!=false){
	            $list_images= $res->data;
	            $list="";
	            foreach ($list_images as $key => $value) {
	            	$list .='@@'.base_url().'uploads/quangcao/' . $cur . '/' .$value->file_name;
	            }
	            $_POST['list_image_other'] = $list;
	        }
			unset($_POST['update']);
                        
			$_POST['upload_time'] = microtime(true);

			$this->quangcao->update($id, $_POST);
			
			$data['success'] = 'Cập nhật thành công';
		}
		$quangcaoinfo = $this->quangcao->getInfo($id);
		$data['id'] = $id;
		$data['quangcao'] = $quangcaoinfo;
		$data["username"] = $this->session->userdata('username');
		$this->load->view('default/admin/quangcaoedit', $data);
	}
        
        function add()
	{
		$data = array();
		if(isset($_POST['add'])) {
			$oldUmask = umask();
            umask(0);
			$cur = date('mY', microtime(true));
			$path = '/' . UPLOADFOLDER . '/quangcao/' . $cur . '/';
            if(!is_dir('.' . $path)) mkdir('.' . $path);
            umask($oldUmask);
			$config['upload_path'] = '.' . $path;
			$config['allowed_types'] = 'jpg|png|gif';
			$this->load->library('upload', $config);
			$image = "";
			if(!$this->upload->do_upload('image')) {
				// $data['error'] = $this->upload->display_errors();
			} else {
				$upload = $this->upload->data();
				$image = $path . $upload['file_name'];
                
                $imagesize = getimagesize('.'.$image);
                //Load thư viện xử lý ảnh để tạo thumbnail
                $this->load->library('image_lib');
                $config['source_image']	= '.'.$image;
                $config['width']    = $imagesize[0];
                $config['height']   = $imagesize[1];
                $config['quality']  = '100%';
                $this->image_lib->initialize($config);
                $this->image_lib->resize();
                $this->image_lib->clear();
			}
			$this->load->library('upload_multi');
	        $res = json_decode($this->upload_multi->do_upload());
	        if($res && $res->status!=false){
	            $list_images= $res->data;
	            $list="";
	            foreach ($list_images as $key => $value) {
	            	$list .='@@'.base_url().'uploads/topics/' . $cur . '/' .$value->file_name;
	            }
	            $_POST['list_image_other'] = $list;
	        }
			unset($_POST['add']);
			$_POST['image'] = $image;
			$_POST['upload_time'] = time();
			$this->quangcao->add($_POST);
			$data['success'] = 'Thêm mới topic thành công';
		}
		$data["username"] = $this->session->userdata('username');
		$this->load->view('default/admin/qlquangcaoadd', $data);
	}

}
